<!DOCTYPE html>
<html>
<head>
    <script src="../../resources/js-test.js"></script>
</head>
<body>
    <a href="#fragment1" id="link1" tabindex="0">link1</a>
    <a href="#fragment2" id="link2" tabindex="0">link2</a>
    <a href="#fragment3" id="link3" tabindex="0">link3</a>
    <a href="#top" id="link4" tabindex="0">link4</a>
    <a href="#" id="link5" tabindex="0">link5</a>
    <a href="#input1" id="link6" tabindex="0">link6</a>
    <br><br>
    <div id="fragment1" name="fragment1" tabindex="0">fragment1</div>
    <div id="fragment2" name="fragment2">fragment2</div>
    <input id="input1" value="abcdef">
    <script>
        description("This tests that if an in-page link is activated, focus control is transferred to the fragment if possible.");

        var link1 = document.getElementById("link1");
        link1.focus();
        debug("Verify that the focus is on the link.");
        shouldBe("document.activeElement", "link1");

        link1.click();
        debug("Click the link and verify that focus has moved to the fragment.");
        shouldBe("document.activeElement", "document.getElementById('fragment1')");

        debug("Move focus back to the link and verify.");
        link1.focus();
        shouldBe("document.activeElement", "link1");

        if (window.testRunner) {
            debug("Send an enter key event which should also trigger focus to move to the fragment.");
            eventSender.keyDown("Enter");
            shouldBe("document.activeElement", "document.getElementById('fragment1')");
        }

        debug("Activate a link that does not have a focusable fragment and verify that the currently focused element is unfocused.");
        var link2 = document.getElementById("link2");
        link2.focus();
        shouldBe("document.activeElement", "link2");
        link2.click();
        shouldBe("document.activeElement", "document.body");

        debug("Activate a link that does not refer to an existing fragment and verify that the currently focused element remains focused.");
        var link3 = document.getElementById("link3");
        link3.focus();
        shouldBe("document.activeElement", "link3");
        link3.click();
        shouldBe("document.activeElement", "link3");

        debug("Activate a link to #top and verify that the link remains focused");
        var link4 = document.getElementById("link4");
        link4.focus();
        shouldBe("document.activeElement", "link4");
        link4.click();
        shouldBe("document.activeElement", "link4");

        debug("Activate a link to # and verify that the link remains focused");
        var link5 = document.getElementById("link5");
        link5.focus();
        shouldBe("document.activeElement", "link5");
        link5.click();
        shouldBe("document.activeElement", "link5");

        debug("Activate a link to an INPUT elemnt, verify that the INPUT is editable");
        var input1 = document.getElementById("input1");
        input1.setSelectionRange(3, 3);
        var link6 = document.getElementById("link6");
        link6.focus();
        shouldBe("document.activeElement", "link6");
        link6.click();
        shouldBe("document.activeElement", "input1");
        if (window.eventSender) {
            eventSender.keyDown('X');
            shouldBeEqualToString("input1.value", "abcXdef");
        }
        var successfullyParsed = true;
    </script>
</body>
</html>
